package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.DiscountLevel;
import cn.wolfcode.crm.domain.MessageHistory;
import cn.wolfcode.crm.mapper.DiscountLevelMapper;
import cn.wolfcode.crm.mapper.MessageHistoryMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDiscountLevelService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DiscountLevelServiceImpl implements IDiscountLevelService {
    @Autowired
    private DiscountLevelMapper mapper;
    @Autowired
    private MessageHistoryMapper messageHistoryMapper;

    public void save(DiscountLevel entity) {
        mapper.insert(entity);
    }

    public void update(DiscountLevel entity) {
        mapper.updateByPrimaryKey(entity);
    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public List<DiscountLevel> list() {
        return mapper.selectAll();
    }

    public DiscountLevel get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public PageResult query(QueryObject qo) {

        //1:查总条数
        int total = mapper.queryForCount(qo);

        if (total == 0) {
            return new PageResult();
        }
        //2:查分页数据
        List<DiscountLevel> rows = mapper.queryForList(qo);

        return new PageResult(total, rows);
    }

    public void saveMessage(String message) {
        MessageHistory messageHistory = new MessageHistory();
        messageHistory.setDate(new Date());
        messageHistory.setIntro(message);
        messageHistoryMapper.insert(messageHistory);
    }


}
